/*
 * Created on Jan 21, 2004
 */
package org.vectrics.company.client;


import org.vectrics.client.webclient.ServerCommandImpl;
import java.util.Vector;
import java.util.Collection;
import org.apache.log4j.*;
import org.vectrics.common.dom.MarshalUtils;
import org.vectrics.company.Company;
import org.vectrics.company.Company;

import org.vectrics.VectricsException;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;



public class FindCompany extends ServerCommandImpl {
	private transient static Logger log = Logger.getLogger(FindCompany.class);
	private Long companyId = null;
	private Company company = null;
	private Collection companyList = new Vector();


	public FindCompany() {
		super();
	}

	public String getCommandCode() {
		return("findCompany");
	}

	protected void prepareRequestData(Element requestElement) {
		log.debug("prepareRequestData()");
		Element companyElement = requestElement.getOwnerDocument().createElement("company");
		requestElement.appendChild(companyElement);
		companyElement.setAttribute("id", companyId.toString());
	}

	public Collection getCompanyList() {
		return(companyList);
	}

	protected void processResponse(Element dataElement) {
		log.debug("processResponse() - translating XML document into user list");
		companyList.clear();
		NodeList nodeList = dataElement.getElementsByTagName("company");
		for (int i = 0; i < nodeList.getLength(); i++){
			Element dataChildElement = (Element)nodeList.item(i); 
			if (log.isDebugEnabled()) {
				log.debug("company element, id = " + dataChildElement.getAttribute("id"));
				log.debug("company element, name = " + dataChildElement.getAttribute("name"));
			}
			MarshalUtils marshaller = MarshalUtils.getInstance();
			Company company = (Company)marshaller.elementToObject(dataChildElement, Company.class);
			companyList.add(company);
			this.company = company;
		}
						
	}


	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}

	public Company getCompany() {
		return company;
	}

}
